package view;

import model.OnScreen;
import model.snippet.Snippet;
import android.annotation.SuppressLint;
import android.content.pm.PackageManager;
import android.graphics.Point;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.widget.RelativeLayout;

import com.vandorenmes.argusv6_5.R;

import controller.SystemController;

/**
 * The activity that holds the augmented reality.
 */
public class Scanner extends ArgusActivity 
{
	private static final String TAG = Scanner.class.getSimpleName();
	//Tag used for following the Android Activity cycle. OnCreate(), OnResume() etc.
	private static final String FLOW = "SystemFlow";
	
	private SystemController _controller;
	private SurfaceHolder _holder;
	
	private CameraView _cameraView;
	
	private ARLayer _ARLayer;
	
	@SuppressLint("ShowToast")
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		Log.d(FLOW, String.format("onCreate() - %s", _controller));
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_scanner);
		
		_cameraView = (CameraView) findViewById(R.id.camera_preview);
		
		Display display = getWindowManager().getDefaultDisplay();
		Point size = new Point();
		display.getSize(size);
		OnScreen.getInstance().setScreenDimension(size.x, size.y);

		RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.main_layout);
		_ARLayer = new ARLayer(this);	
		mainLayout.addView(_ARLayer);
		
		_connected = true;		
	}

	/**
	 * Add a snippet to the view.
	 * @param newSnippet The snippet to add.
	 */
	public void addSnippetView(Snippet newSnippet) 
	{
		_ARLayer.addSnippetView(newSnippet);
	}

	public void startCamera(SurfaceHolder surfaceHolder)
	{
		Log.d(FLOW, String.format("startCamera()"));
		_holder = surfaceHolder;
		if(_controller == null)
			_controller = new SystemController(this);
		_controller.startCamera(surfaceHolder);
	}
	
	@Override
	protected void onResume() 
	{
		Log.d(FLOW, String.format("onResume()"));
		super.onResume();
		if(_controller == null)
			_controller = new SystemController(this);
		
		_controller.resumeSensorController();
		_controller.resumeCamera(_holder);
		
		OnScreen.getInstance().getWebServiceConnector().connectToServer(this);
	}
	
	@Override
    protected void onPause() 
	{
		Log.d(FLOW, String.format("onPause()"));
	    super.onPause();
	     
	    _ARLayer.invalidate();
	    	    
	    _controller.pauseSensorController();
	    _controller.stopCamera();
	    
	    OnScreen.getInstance().getWebServiceConnector().disconnectFromServer();
	}
	
	@Override
	protected void onStop() 
	{
		Log.d(FLOW, String.format("onStop()"));
		onPause();
		super.onStop();
	}
	
	@Override
	protected void onDestroy() 
	{
		Log.d(FLOW, String.format("onDestroy()"));
		onPause();
		super.onDestroy();
			
//		OnScreen.getInstance().getWebServiceConnector().disconnectFromServer();
		System.exit(0);
	}
	
	/**
	 * Draw points corresponding to the reference points
	 * @param points The location of the currently visible reference points.
	 */
	public void drawPoints(float[] points)
	{
		_cameraView.setPoints(points);
	}
	
	/**
	 * Toggles the flashlight for use in case of bad lighting.
	 */
	@Override
	public boolean onTouchEvent(MotionEvent event) 
	{		
		if(super.onTouchEvent(event))
			return true;
		
		if(event.getAction() == MotionEvent.ACTION_DOWN)
		{
			//Camera has flash
			if(getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH))
			{
				Log.d(TAG, "TOUCH! - FLASH!");
				_controller.toggleFlashLight();
			}
			return super.onTouchEvent(event);
		}
		
		return false;
	}
}